package com.yxplearn.mybatisplus.manager;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxplearn.mybatisplus.mapper.UserMapper;
import com.yxplearn.mybatisplus.pojo.User;
import com.yxplearn.mybatisplus.pojo.Users;
import com.yxplearn.mybatisplus.util.IDGenerator;


@Component
public class UserMapperManager extends CachedCompositeDataManager<Users> {
    @Resource
    UserMapper userMapper;

    private static IDGenerator idGenerator = IDGenerator.newGenerator();
    
    @Override
    protected Users getFromDB(long playerId) {
//        String key = String.format(RedisKeys.Private.ACHIEVEMENT, playerId);
//        Optional<Map<String, Achievement>> opt = RedisUtil.getOneToMany(playerId, key);
//        List<Achievement> list = opt.map(mp -> {
//            List<Long> dbIds = dao.getIdList(playerId);
//            if (mp.size() < dbIds.size()) {
//                List<Long> absentIds = dbIds.stream().filter(id -> !mp.containsKey(String.valueOf(id))).collect(Collectors.toList());
//                List absent = dao.getList(playerId, absentIds);
//                RedisUtil.setOneToMany(playerId, key, absent);
//
//                List<Achievement> list2 = new ArrayList<>(mp.size() + absent.size());
//                list2.addAll(mp.values());
//                list2.addAll(absent);
//                return list2;
//            } else {
//                return new ArrayList<>(mp.values());
//            }
//        }).orElseGet(() -> dao.get(playerId));
//        Users achievements = new Users(playerId, list);
//        return achievements;
    	QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
    	List<User> userList = userMapper.selectList(queryWrapper.eq("id", playerId));
		Users users = new Users(playerId, userList);
		return users;
    }

    @Override
    protected void updatePartOfBatchParameterPersistEntities(List<PersistEntity> parameterPersistEntityPartList) {
    	for (PersistEntity persistEntity : parameterPersistEntityPartList) {
    		userMapper.insert((User)persistEntity);
		}
    }

    @Override
    protected void deletePartOfBatchDeletedIds(List<Long> deletedIdList) {
    	userMapper.deleteBatchIds(deletedIdList);
    }
//
//    @Override
//    public void onUpdate(int playerId, List<PersistEntity> persistEntityList) {
//        String key = String.format(RedisKeys.Private.ACHIEVEMENT, playerId);
//        RedisUtil.setOneToMany(playerId, key, persistEntityList);
//    }
//
//    @Override
//    public void onDelete(int playerId, List<Long> delPIDList) {
//        String key = String.format(RedisKeys.Private.ACHIEVEMENT, playerId);
//        RedisUtil.delOneToMany(playerId, key, delPIDList);
//    }
//
//    /**
//     * @return 生成一个新的id, 此id是全服唯一的
//     */
//    public static long generateId() {
//        return idGenerator.nextId();
//    }
    
}
